namespace ASPNET.StarterKit.IssueTracker.BusinessLogicLayer
{
    using ASPNET.StarterKit.IssueTracker.DataAccessLayer;
    using System;

    public class IssueAttachment
    {
        private byte[] _Attachment;
        private string _ContentType;
        private string _CreatorDisplayName;
        private string _CreatorUsername;
        private DateTime _DateCreated;
        private string _FileName;
        private int _Id;
        private int _IssueId;

        public IssueAttachment(string fileName, string contentType, byte[] attachment) : this(DefaultValues.GetIssueAttachmentIdMinValue(), DefaultValues.GetIssueIdMinValue(), string.Empty, string.Empty, DefaultValues.GetDateTimeMinValue(), fileName, contentType, attachment)
        {
        }

        public IssueAttachment(int issueId, string creatorUsername, string fileName, string contentType, byte[] attachment) : this(DefaultValues.GetIssueAttachmentIdMinValue(), issueId, creatorUsername, string.Empty, DefaultValues.GetDateTimeMinValue(), fileName, contentType, attachment)
        {
        }

        public IssueAttachment(int attachmentId, int issueId, string creatorUsername, string creatorDisplayName, DateTime created, string fileName) : this(attachmentId, issueId, creatorUsername, creatorDisplayName, created, fileName, string.Empty, null)
        {
        }

        public IssueAttachment(int attachmentId, int issueId, string creatorUsername, string creatorDisplayName, DateTime created, string fileName, string contentType, byte[] attachment)
        {
            this._Id = attachmentId;
            this._IssueId = issueId;
            this._CreatorUsername = creatorUsername;
            this._CreatorDisplayName = creatorDisplayName;
            this._DateCreated = created;
            this._FileName = fileName;
            this._ContentType = contentType;
            this._Attachment = attachment;
        }

        public static void DeleteIssueAttachment(int attachmentId)
        {
            if (attachmentId <= DefaultValues.GetIssueAttachmentIdMinValue())
            {
                throw new ArgumentOutOfRangeException("attachmentId");
            }
            DataAccessLayerBaseClassHelper.GetDataAccessLayer().DeleteIssueAttachment(attachmentId);
        }

        public static IssueAttachment GetIssueAttachmentById(int attachmentId)
        {
            if (attachmentId <= DefaultValues.GetIssueAttachmentIdMinValue())
            {
                throw new ArgumentOutOfRangeException("attachmentId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssueAttachmentById(attachmentId);
        }

        public static IssueAttachmentCollection GetIssueAttachmentsByIssueId(int issueId)
        {
            if (issueId <= DefaultValues.GetIssueIdMinValue())
            {
                throw new ArgumentOutOfRangeException("issueId");
            }
            return DataAccessLayerBaseClassHelper.GetDataAccessLayer().GetIssueAttachmentsByIssueId(issueId);
        }

        public bool Save()
        {
            int TempId = DataAccessLayerBaseClassHelper.GetDataAccessLayer().CreateNewIssueAttachment(this);
            if (TempId > 0)
            {
                this._Id = TempId;
                IssueNotification.SendIssueNotifications(this.IssueId);
                return true;
            }
            return false;
        }

        public byte[] Attachment
        {
            get
            {
                return this._Attachment;
            }
        }

        public string ContentType
        {
            get
            {
                if ((this._ContentType == null) | (this._ContentType.Length == 0))
                {
                    return string.Empty;
                }
                return this._ContentType;
            }
        }

        public string CreatorDisplayName
        {
            get
            {
                if ((this._CreatorDisplayName == null) | (this._CreatorDisplayName.Length == 0))
                {
                    return string.Empty;
                }
                return this._CreatorDisplayName;
            }
        }

        public string CreatorUsername
        {
            get
            {
                if ((this._CreatorUsername == null) | (this._CreatorUsername.Length == 0))
                {
                    return string.Empty;
                }
                return this._CreatorUsername;
            }
        }

        public DateTime DateCreated
        {
            get
            {
                return this._DateCreated;
            }
        }

        public string FileName
        {
            get
            {
                if ((this._FileName == null) | (this._FileName.Length == 0))
                {
                    return string.Empty;
                }
                return this._FileName;
            }
        }

        public int Id
        {
            get
            {
                return this._Id;
            }
        }

        public int IssueId
        {
            get
            {
                return this._IssueId;
            }
            set
            {
                if (value <= DefaultValues.GetIssueIdMinValue())
                {
                    throw new ArgumentOutOfRangeException("value");
                }
                this._IssueId = value;
            }
        }
    }
}

